//
// Created by 崔世强 on 2018/3/5.
// Copyright (c) 2018 Facebook. All rights reserved.
//

import Foundation
import UIKit
import SnapKit

enum ChatMoreType {
    case pic
    case camera
    case file
}

fileprivate let kMoreCellNumberOfOneRow = 4
fileprivate let kMoreCellRow = 2
fileprivate let kMoreCellNumberOfOnePage = kMoreCellRow * kMoreCellNumberOfOneRow
fileprivate let kMoreCellID = "moreCellID"

let kChatKeyboardBgColor: UIColor? = UIColor(named: "chat_input_bg")
let kChatBarOriginHeight: CGFloat = 49.0
let kChatBarTextViewMaxHeight: CGFloat = 100
let kChatBarTextViewHeight: CGFloat = kChatBarOriginHeight - 14.0

protocol ChatMoreViewDelegate: class {
    func cellOnClick(moreView: ChatMoreView, selectType: ChatMoreType)
}

class ChatMoreView: UIView {
    weak var delegate:ChatMoreViewDelegate?

    lazy var moreView: UICollectionView = { [unowned self] in
        let collectionV = UICollectionView(frame: CGRect.zero, collectionViewLayout: ChatHorizontalLayout(column: kMoreCellNumberOfOneRow, row: kMoreCellRow))
        collectionV.backgroundColor = kChatKeyboardBgColor
        collectionV.dataSource = self
        collectionV.delegate = self
        return collectionV
    }()

    lazy var pageControl: UIPageControl = { [unowned self] in
        let pageC = UIPageControl()
        pageC.numberOfPages = self.moreDataSouce.count / kMoreCellNumberOfOnePage + (self.moreDataSouce.count % kMoreCellNumberOfOnePage == 0 ? 0 : 1)
        pageC.currentPage = 0
        pageC.pageIndicatorTintColor = UIColor.lightGray
        pageC.currentPageIndicatorTintColor = UIColor.gray
        return pageC
    }()

    lazy var moreDataSouce: [(name: String, icon: UIImage, type: ChatMoreType)] = {
        return [
            ("照片", #imageLiteral(resourceName: "sharemore_pic"), ChatMoreType.pic),
            ("拍摄", #imageLiteral(resourceName: "sharemore_video"), ChatMoreType.camera),
            ("文件", #imageLiteral(resourceName: "sharemore_file"), ChatMoreType.file)
        ]
    }()

    override func layoutSubviews() {
        super.layoutSubviews()

        self.addSubview(moreView)
        self.addSubview(pageControl)

        moreView.snp.makeConstraints { (make) in
            make.left.top.right.equalTo(self)
            make.bottom.equalTo(pageControl.snp.top)
        }

        pageControl.snp.makeConstraints { (make) in
            make.left.right.equalTo(self)
            make.height.equalTo(30)
            make.bottom.equalTo(self.snp.bottom)
        }
        self.backgroundColor = kChatKeyboardBgColor
        moreView.contentSize = CGSize(width: kScreenW * 2, height: moreView.height)
        // 注册itemID
        moreView.register(ChatMoreViewCell.self, forCellWithReuseIdentifier: kMoreCellID)

        let line = UIView()
        self.addSubview(line)
        line.backgroundColor = .systemGroupedBackground
//        line.backgroundColor = UIColor.black
        line.snp.makeConstraints { make in
            make.left.right.top.equalTo(self)
            make.height.equalTo(0.5)
        }
    }
}

extension ChatMoreView : UICollectionViewDataSource, UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return moreDataSouce.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let moreModel = moreDataSouce[indexPath.item]

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kMoreCellID, for: indexPath) as? ChatMoreViewCell

        cell?.model = moreModel

        return cell!
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let moreModel = moreDataSouce[indexPath.item]
        delegate?.cellOnClick(moreView: self, selectType: moreModel.type)
    }
}

extension ChatMoreView: UIScrollViewDelegate {
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let contentOffset = scrollView.contentOffset.x
        let page = contentOffset / scrollView.frame.size.width + (Int(contentOffset) % Int(scrollView.frame.size.width) == 0 ? 0 : 1)
        pageControl.currentPage = Int(page)
    }
}
